package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.OrderComment;
import com.briup.jz.bean.OrderCommentExample;
import com.briup.jz.dao.OrderCommentMapper;
import com.briup.jz.service.IOrderCommentService;
import com.briup.jz.utils.CustomerException;
@Service
public class OrderCommentService implements IOrderCommentService{
	@Autowired
	private OrderCommentMapper orderCommentMapper;
	
	@Override
	public void savaOrUpdate(OrderComment orderComment) throws CustomerException {
		if(orderComment.getId()!=null) {       //有id就是更新
			orderCommentMapper.updateByPrimaryKey(orderComment);
		} else {//判断插入数据的
			// 判断是否有同名的分类，如果有抛出异常
			OrderCommentExample example = new OrderCommentExample();
			example.createCriteria().andCustomerIdEqualTo(orderComment.getCustomerId());
			List<OrderComment> list = orderCommentMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");	
			}
			orderCommentMapper.insert(orderComment);
		}
		
	}

	@Override
	public List<OrderComment> query(String content) {
		OrderCommentExample example = new OrderCommentExample();//创建模板
		if(content!=null){
			example.createCriteria().andContentLike("%"+content+"%");
		}
		List<OrderComment> list = orderCommentMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		OrderComment orderComment =orderCommentMapper.selectByPrimaryKey(id);
		if(orderComment == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		orderCommentMapper.deleteByPrimaryKey(id);
	}

}
